﻿/**====================================
 *Copyright(C) 2018 by Wipace 
 *All rights reserved. 
 *FileName:     .cs 
 *Author:       CGzhao 
 *Version:      1.0 
 *UnityVersion：2018.2.3 
 *Date:         2018-12-11 
 *Email:		1341674064@qq.com
 *Description:  生成AssetBundles文件工具  
 *History: 
======================================**/

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class BuildAllAssetBundles
{
    //[MenuItem("Tools/Build AssetBunldes")]   在unity编辑器视图生成一个Tools -> Build AssetBundles 选项，点击可以执行下面的方法
    private const string path = "Assets/AssetBundles/";
    private const string outputPath = "AssetBundles";
    [MenuItem(path + "构建所有AssetBundles")]
    static void CreateAllAssetBundles()
    {
        //string dir = Application.dataPath.Replace("Assets", "AssetBundles");
        Debug.Log("dir: " + outputPath);
        if (Directory.Exists(outputPath) == false)        //如果不存在AssetBundles文件夹，则生成AssetBundles文件夹
        {
            Directory.CreateDirectory(outputPath);
        }

        BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.Android);
    }

    [MenuItem(path + "删除所有AssetBundles")]
    static void DeleteAllAssetBundle()
    {
        if (!Directory.Exists(outputPath) )    return;
        //目录为空，删除目录
        DirectoryInfo directoryInfo = new DirectoryInfo(outputPath);
        //如果有子目录，先循环删除子目录，再删除当前目录
        directoryInfo.Delete(true);
    }
}
